package com.kingdee.pda.util;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * 软键盘相关工具类
 * @author LiJiaJian
 * @date 2016/5/20 15:26
 */
public class KeyboardUtils {

    private KeyboardUtils() {
        throw new AssertionError();
    }

    /**
     * 针对输入框打开软键盘
     * @param editText 输入框
     * @param context 上下文
     */
    public static void openSoftKeybord(EditText editText, Context context){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 针对输入框关闭软键盘
     * @param editText 输入框
     * @param context 上下文
     */
    public static void closeSoftKeybord(EditText editText, Context context){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    /**
     * 全局隐藏软键盘
     * @param activity
     */
    public static void closeSoftKeybord(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);// 隐藏软键盘
        }
    }
}
